<!--suppress ALL -->
<div class="layui-fluid layui-anim website-anim" id="website-lifecycle-entrust" lay-title="课题计划管理"
     xmlns:th="http://www.w3.org/1999/xhtml">
    <div class="layui-row website-container">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body website-table-full">
                    <input type="hidden" id="topicClassifyCode" autocomplete="off" class="layui-input" data-th-value="${topicClassifyCode}"/>
                    <form class="layui-form layui-table-form" lay-filter="lifecycle-plan-table-form">
                        <div class="layui-row">
                            <div class="layui-col-md10">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label">期次</label>
                                        <div class="layui-input-inline" style="width: 60px;">
                                            <input type="text" name="year" id="year"  autocomplete="off" class="layui-input">
                                        </div>
                                        <div th:if="${topicClassifyCode}=='entrust'" class="layui-form-mid">-</div>
                                        <div th:if="${topicClassifyCode}=='entrust'" class="layui-input-inline">
                                            <select name="planNo">
                                                <option
                                                        th:each="item : ${planNos}"
                                                        th:value="${item.code}" th:text="${item.name}">
                                                </option>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md2 layui-col-sm12 layui-col-xs12 table-action-area">
                                <div class="layui-btn layui-btn-sm layui-btn-primary website-button-blue-plain table-action"
                                     id="query">
                                    查询
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary website-button-green-plain table-action"
                                     id="exportEntrustTopic">
                                    结项统计
                                </div>
                            </div>
                        </div>
                    </form>
                    <table lay-filter="lifecycleEntrustTable" lay-data="{id: 'lifecycleEntrustTable'}"></table>
                </div>
            </div>
        </div>
    </div>
</div>

<!--表格中的操作栏-->
<script type="text/html" id="lifecycle-plan-option">
    {{# if (d.statusCode === 'create' && d.topicClassifyCode === 'entrust') { }}
    <a lay-event="chooseEntrust" class="website-green" shiro:hasPermission="lifecycle:list">课题分派</a>
    <a lay-event="endAssign" class="website-green" shiro:hasPermission="lifecycle:list">结束分派</a>
    {{# } else if(d.statusCode === 'create' && d.topicClassifyCode === 'annual') { }}
    <a lay-event="endCheck" class="website-green" shiro:hasPermission="lifecycle:list">结束审查</a>
    {{# } else if(d.statusCode === 'check' || d.statusCode === 'assign' ) { }}
    <a lay-event="endSubject" class="website-green" shiro:hasPermission="lifecycle:list">结束学科分类</a>
    {{# } else if(d.statusCode === 'subject') { }}
    <a lay-event="endExpert" class="website-green" shiro:hasPermission="lifecycle:list">结束专家分配</a>
    {{# } else if(d.statusCode === 'expert') { }}
    <a lay-event="endReview" class="website-green" shiro:hasPermission="lifecycle:list">结束评审</a>
    {{# } }}
    {{# if(d.statusCode !== 'create' && d.topicClassifyCode === 'entrust') { }}
    <a lay-event="showEntrust" class="website-green" shiro:hasPermission="lifecycle:list">查看结项课题</a>
    {{# } }}
    <a lay-event="resumeEntrust" class="website-green" shiro:hasPermission="lifecycle:resume">恢复</a>
</script>


<script type="text/html" id="name">
    <span>委托课题结项鉴定期次</span>
</script>

<script type="text/html" id="statusCode">
    {{#
    var dict=layui.dict.getByCode(d.statusCode);
    }}
    {{#  if(dict){ }}
    <span>{{ dict.name }}</span>
    {{#  } }}
</script>

<script data-th-inline="none" type="text/javascript">
    layui.use(['jquery', 'form', 'table', 'website','laydate','dict'], function () {
        var $ = layui.jquery,
                website = layui.website,
                form = layui.form,
                table = layui.table,
                laydate = layui.laydate,
            $view = $('#website-lifecycle-entrust'),
            $query = $view.find('#query'),
            $reset = $view.find('#reset'),
            $exportEntrustTopic = $view.find('#exportEntrustTopic'),
            $searchForm = $view.find('form'),
                sortObject = {field: 'createTime', type: null},
                tableIns;
        // var getTpl = chooseStageLife.innerHTML,chooseStageView = document.getElementById('chooseStageLifeView');
        // laytpl(getTpl).render($("#topicClassifyCode").val(), function(html){
        //     chooseStageView.innerHTML = html;
        // });
        form.render();
        initTable();
        //初始化年份的搜索框
        laydate.render({
            elem: '#year', //指定元素
            type: 'year',
            trigger: 'click',
            value: new Date()
        });
        /**
         * table操作栏的按钮监听事件
         **/
        table.on('tool(lifecycleEntrustTable)', function (obj) {
            var data = obj.data, layEvent = obj.event;
            var loading = layer.load(0, { time:10*1000, shade: [0.1, '#fff'] });
            if (layEvent === 'endCheck') {
                website.ajaxPost(ctx + 'lifecyclePlan/' + data.planId + '/check',null, function (data) {
                    layer.close(loading);
                    website.alert.success(data.data);
                    $('#website-lifecycle-entrust').find('#query').click();
                });
            } else if(layEvent === 'endAssign') {
                website.ajaxPost(ctx + 'lifecyclePlan/' + data.planId + '/assign',null, function (data) {
                    layer.close(loading);
                    website.alert.success(data.data);
                    $('#website-lifecycle-entrust').find('#query').click();
                });
            } else if(layEvent === 'endSubject') {
                website.ajaxPost(ctx + 'lifecyclePlan/' + data.planId + '/subject', $("#topicClassifyCode").val(), function (data) {
                    layer.close(loading);
                    website.alert.success(data.data);
                    $('#website-lifecycle-entrust').find('#query').click();
                });
            } else if(layEvent === 'endExpert') {
                website.ajaxPost(ctx + 'lifecyclePlan/' + data.planId + '/expert',null, function (data) {
                    layer.close(loading);
                    website.alert.success(data.data);
                    $('#website-lifecycle-entrust').find('#query').click();
                });
            } else if(layEvent === 'endReview') {
                website.ajaxPost(ctx + 'lifecyclePlan/' + data.planId + '/review',null, function (data) {
                    layer.close(loading);
                    website.alert.success(data.data);
                    $('#website-lifecycle-entrust').find('#query').click();
                });
            } else if(layEvent === 'chooseEntrust') {
                layer.close(loading);
                website.navigate('/biz/lifecyclePlan/'+ data.planId + '/chooseEntrusts');
                // website.modal.view('选择结项课题', 'biz/lifecyclePlan/'+ data.planId + '/entrusts/' , {
                //     area: $(window).width() <= 750 ? '95%' : '80%'
                // });
            } else if(layEvent === 'showEntrust') {
                layer.close(loading);
                website.modal.view('查看结项课题', 'biz/lifecyclePlan/'+ data.planId + '/showEntrusts/' , {
                    area: $(window).width() <= 750 ? '95%' : '80%'
                });
            } else if(layEvent === 'resumeEntrust') {
                website.ajaxPost(ctx + 'lifecyclePlan/' + data.planId + '/resume',null, function (data) {
                    layer.close(loading);
                    website.alert.success(data.data);
                    $('#website-lifecycle-entrust').find('#query').click();
                });
            }
        });

        /**
         * 表格的排序点击
         **/
        table.on('sort(lifecycleEntrustTable)', function (obj) {
            sortObject = obj;
            tableIns.reload({
                initSort: obj,
                where: $.extend(getQueryParams(), {
                    field: obj.field,
                    order: obj.type
                })
            });
        });

            $query.on('click', function () {
        var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
        tableIns.reload({where: params, page: {curr: 1}});
        });

            $reset.on('click', function () {
            $searchForm[0].reset();
        sortObject.type = 'null';
        createTimeTo = null;
        createTimeFrom = null;
        tableIns.reload({where: getQueryParams(), page: {curr: 1}, initSort: sortObject});
        });

        $exportEntrustTopic.on('click', function () {
    var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
    if (!params.year) {
    website.alert.warn('请选择需导出数据的期次');
    return;
    }
    website.download(ctx + 'biz/lifecycle/'+ $("#topicClassifyCode").val() + '/exportEntrustTopic', params, '中国法学会' + params.year + '期次结项课题统计表.xls');
        });

        /**
         * 初始化表格
         */
        function initTable() {
            tableIns = website.table.init({
                elem: $view.find('table'),
                id: 'lifecycleEntrustTable',
                url: ctx + 'biz/lifecycle/'+ $("#topicClassifyCode").val() + '/list',
                where: {year: new Date().getFullYear(), planNo:'001'},
                cols: [[
                    /*{type: 'checkbox'},*/
                    /* { type: 'numbers', title: '序号', },*/
                    {field: 'period', title: '期次', minWidth: 100},
                    {field: 'name', title: '名称',templet: '#name',  minWidth: 100},
                    {field: 'status', title: '状态',templet: '#statusCode',  minWidth: 100},
                    {title: '操作', toolbar: '#lifecycle-plan-option', minWidth: 140}
                ]]
            });
        }

        /**
         * 获取查询条件
         * @returns {{}}
         */
        function getQueryParams() {
            var params = $searchForm.serializeJson();
            return params;
        }

    })
</script>